package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.ScoreLotteryDao;
import com.qdlc.p2p.dal.dto.ScoreLottery;
import com.qdlc.p2p.dal.model.ScoreLotteryModel;

@Repository("scoreLotteryDao")
public class ScoreLotteryImpl extends BaseDaoImpl<ScoreLottery> implements ScoreLotteryDao {

	@Override
	public double getMinRate() {
		String jpql = "select min(rate) from " + Global.DB_PREFIX + "score_lottery where status = "+ ScoreLotteryModel.STATUS_ENABLE;
		Query query = em.createNativeQuery(jpql);
		double result = 0;
		Object obj = query.getSingleResult();
		if (obj != null) {
			result = Double.parseDouble(obj.toString());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ScoreLottery> getAllLottery() {
		String jpql = "from ScoreLottery where status = "+ ScoreLotteryModel.STATUS_ENABLE;
		Query query = em.createQuery(jpql);
		return query.getResultList();
	}
	
	@Override
	public double getTotalRate() {
		String jpql = "select sum(rate) from " + Global.DB_PREFIX + "score_lottery where status = "+ ScoreLotteryModel.STATUS_ENABLE;
		Query query = em.createNativeQuery(jpql);
		double result = 0;
		Object obj = query.getSingleResult();
		if (obj != null) {
			result = Double.parseDouble(obj.toString());
		}
		return result;
	}
}
